<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (config('app.debug')) {
            \DB::listen(function ($query) {
                $sql = str_replace('?', '"' . '%s' . '"', $query->sql);

                $qBindings = [];
                foreach ($query->bindings as $key => $value) {
                    if (is_numeric($key)) {
                        $qBindings[] = $value;
                    } else {
                        $sql = str_replace(':' . $key, '"' . $value . '"', $sql);
                    }
                }

                try {
                    $sql = vsprintf($sql, $qBindings);
                } catch (\Exception $e) {

                }
                $sql = str_replace("\\", "", $sql);
                \Log::debug('[Execute SQL] query_time=' . $query->time . ' ms; sql=' . $sql . ';');
            });
        }
        Blade::withoutDoubleEncoding();
    }
}
